package starfire.gui.ftpModelAndRenderer;

import java.io.*;

import javax.swing.table.*;

import starfire.ftp.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author zzzhc.starfire@gmail.com
 * @version 1.0
 */

public class FtpTableModel
    extends javax.swing.table.DefaultTableModel {
  File dir = null;
  public static String[] headers = {
      "Name", "Date", "Time", "Length", "Attribute"};
  public static int NAMECOLUMN = 0;
  public static int DATECOLUMN = 1;
  public static int TIMECOLUMN = 2;
  public static int LENGTHCOLUMN = 3;
  public static int ATTRIBUTECOLUMN = 4;

  public FtpTableModel(FtpFile dir) {
    super(headers, headers.length);
    this.dir = dir;
    addSubFile(dir);
  }

  protected void addSubFile(FtpFile dir) {
    if (dir.isFile()) {
      return;
    }
    FtpFile[] fs = (FtpFile[]) (dir.listFiles());
    dataVector.clear();
    for (int i = 0; i < fs.length; i++) {
      if (fs[i].isDirectory()) {
        dataVector.add(fs[i]);
      }
    }
    for (int i = 0; i < fs.length; i++) {
      if (fs[i].isFile()) {
        dataVector.add(fs[i]);
      }
    }
  }

  public String getColumnName(int column) {
    if (column < headers.length) {
      return headers[column];
    }
    return null;
  }

  public Class getColumnClass(int columnIndex) {
    if (columnIndex == NAMECOLUMN) {
      return FtpFile.class;
    }
    if (columnIndex == DATECOLUMN) {
      return String.class;
    }
    if (columnIndex == TIMECOLUMN) {
      return String.class;
    }
    if (columnIndex == LENGTHCOLUMN) {
      return Long.class;
    }
    if (columnIndex == ATTRIBUTECOLUMN) {
      return String.class;
    }
    return String.class;
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
 //   return columnIndex == NAMECOLUMN;
      return false;
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    FtpFile f = (FtpFile) (dataVector.get(rowIndex));
    if (f == null) {
      return null;
    }
    if (columnIndex == NAMECOLUMN) {
      return f;
    }
    if (columnIndex == DATECOLUMN) {
      return f.getCreateTime();
    }
    if (columnIndex == TIMECOLUMN) {
      return f.getCreateTime();
    }
    if (columnIndex == LENGTHCOLUMN) {
      return new Long(f.length());
    }
    if (columnIndex == ATTRIBUTECOLUMN) {
      return f.getAttribute();
    }
    return null;
  }

  public void setValueAt(Object aValue, int row, int column) {
    if (column != 0) {
      return;
    }
    FtpFile f = (FtpFile) dataVector.elementAt(row);
    // File temp = new File(f.getParent()+f.separatorChar+((File)aValue).getName());
    // f.renameTo(temp);
    // dataVector.set(row,temp);
    dataVector.set(row, aValue);
  }


}
